import { defineStore } from 'pinia'; 
import { toRaw } from 'vue'; 

export const useUserStore = defineStore('user', {
  state: () => ({
    isLoggedIn: false,
    isGuest: false,
    userInfo: {
      username: '',
      signature: '',
      token: '',
      userAvatar :'',
    },
      
    previewImage :''
  }),
  actions: {
    async initData() {
      try {
        const storedUser = JSON.parse(localStorage.getItem('user'));
        if (storedUser) {
          this.userInfo = storedUser;
          this.isLoggedIn = !!storedUser.token;
          this.isGuest = false;
        }

        const mainUserInfo = await window.electronAPI.getUserInfo();
        if (mainUserInfo) {
          this.userInfo = mainUserInfo;
          this.isLoggedIn = !!mainUserInfo.token;
          this.isGuest = false;
          localStorage.setItem('user', JSON.stringify(mainUserInfo));
        }
      } catch (error) {
        console.error('初始化用户数据失败:', error);
      }
    },
    setupUserInfoListener() {
      window.electronAPI.onUserInfoUpdated((updatedInfo) => {
        
        this.userInfo = { ...updatedInfo };
        console.log("updateInfo",updatedInfo)
        this.isLoggedIn = !!updatedInfo.token;
        this.isGuest = false;
        localStorage.setItem('user', JSON.stringify(updatedInfo));
      });
    },
    login(userData) {
      this.isLoggedIn = true;
      this.isGuest = false;
      this.userInfo = { ...userData };
      localStorage.setItem('user', JSON.stringify(this.userInfo));
      window.electronAPI.updateUserInfo(toRaw(this.userInfo));
    },
    setData(userData) {
      this.userInfo = userData;
      localStorage.setItem('user', JSON.stringify(userData));
      window.electronAPI.updateUserInfo(toRaw(this.userInfo));
    },
    updateProfile(updatedData) {
      this.userInfo = {
        username: updatedData.username,
        signature: updatedData.signature,
        token: this.userInfo.token,
        userAvatar: updatedData.userAvatar
      };
      localStorage.setItem('user', JSON.stringify(this.userInfo));
      window.electronAPI.updateUserInfo(toRaw(this.userInfo));
    },
    setUserAvatar(state, image) {
      state.userAvatar = image; 
      localStorage.setItem('userAvatar', image);
    },
    setPreviewImage(state, image) {
      state.previewImage = image; 
    },
    loadUserAvatar(state) {
      const savedImage = localStorage.getItem('userAvatar');
      if (savedImage) {
        state.userAvatar = savedImage; 
      }
    },
    loginAsGuest() {
      this.isLoggedIn = false;
      this.isGuest = true;
      this.userInfo = {
        username: '',
        signature: '',
      };
      localStorage.setItem('user', JSON.stringify(this.userInfo));
      window.electronAPI.updateUserInfo(toRaw(this.userInfo));
    },
    logout() {
      this.isLoggedIn = false;
      this.isGuest = false;
      this.userInfo = {
        username: this.userInfo.username,
        signature: this.userInfo.signature,
        token: '',
      };
      localStorage.setItem('user', JSON.stringify(this.userInfo));
      window.electronAPI.updateUserInfo(toRaw(this.userInfo));
    },
    async initializeUser() {
      await this.initData();
      this.setupUserInfoListener();
    },
  },
});
